﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Data;
using System.Text;
using System.Drawing;
using System.Drawing.Text;
using System.Drawing.Drawing2D;

namespace Shapefile
{
    public class ESRIPolyLine : ESRIShape
    {
        internal RectangleD boundingBox_;

        internal List<PointD[]> parts_;

        //protected internal ESRIPolyLine(int recordNumber, StringDictionary metaData, IDataRecord dataRecord)
        //    : base(ShapeType.PolyLine, recordNumber, metaData, dataRecord){ }

        protected internal ESRIPolyLine(int recordNumber, StringDictionary metaData, IDataRecord dataRecord, byte[] shapeData)
            : base(ShapeType.PolyLine, recordNumber, metaData, dataRecord)
        {
            ParsePolyLineOrPolygon(shapeData, out boundingBox_, out parts_);
        }

        public RectangleD BoundingBox
        {
            get { return boundingBox_; }
        }

        public List<PointD[]> Parts
        {
            get { return parts_; }
        }

        public override void Render(Graphics g)
        {
            float sx = g.Transform.Elements[0];
            float sy = g.Transform.Elements[3];
            float pen_width = 1.0f / (sx - sy) * 2;
            Pen pen = new Pen(Color.Blue, pen_width);

            for (int i = 0; i < parts_.Count; i++)
            {
                GraphicsPath path = new GraphicsPath();
                PointF[] temp = new PointF[parts_[i].Length];
                for (int j = 0; j < parts_[i].Length; j++)
                {
                    temp[j].X = Convert.ToSingle(parts_[i][j].X);
                    temp[j].Y = Convert.ToSingle(parts_[i][j].Y);
                }
                path.FillMode = FillMode.Alternate;
                path.AddLines(temp);
                g.DrawPath(pen, path);
            }
        }
    }
}
